﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Management;
using System.Text;
using System.Threading.Tasks;

namespace Common
{
    /// <summary>
    /// 计算机信息类
    /// </summary>
    public class ComputerInfo
    {
        /// <summary>
        /// 获取CPU模块
        /// </summary>
        /// <returns> </returns>
        public static string GetCpuModel()
        {
            try
            {
                using (ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_Processor"))
                {
                    foreach (ManagementObject obj in searcher.Get())
                    {
                        return obj["Name"].ToString();
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error getting CPU information: " + ex.Message);
            }

            return null;
        }

        /// <summary>
        /// 获取主板序列号
        /// </summary>
        /// <returns> </returns>
        public static string GetMainBoardSN()
        {
            string sn = "";
            ManagementClass mc = new ManagementClass("win32_baseboard");
            ManagementObjectCollection moc = mc.GetInstances();
            foreach (ManagementObject obj in moc)
            {
                sn = obj.Properties["SerialNumber"].Value.ToString();
                break;
            }
            return sn;
        }

        /// <summary>
        /// 获取BIOS系统UUID
        /// </summary>
        /// <returns> </returns>
        public static string GetBiosUUID()
        {
            string code = null;
            SelectQuery query = new SelectQuery("select * from Win32_ComputerSystemProduct");
            using (ManagementObjectSearcher searcher = new ManagementObjectSearcher(query))
            {
                foreach (var item in searcher.Get())
                {
                    using (item) code = item["UUID"].ToString();
                }
            }
            return code;
        }
    }
}